package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.company.entity.Duty;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.oa.core.entity.com.*;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.core.service.com.*;
import com.fz.us.oa.core.service.common.FileManagerService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by dzw on 2018/3/19.
 * 获取数据
 */
@Controller
@RequestMapping("/com/data")
public class DataController extends BaseController {
    private static final long serialVersionUID = 7227412966159033820L;

    @Autowired
    private PersonService personService;
    @Autowired
    private DirectorService directorService;
    @Autowired
    private ResidentService residentService;
    @Autowired
    private StudyService studyService;
    @Autowired
    private WorkService workService;
    @Autowired
    private FamilyService familyService;
    @Autowired
    private FileManagerService fileManagerService;

    /**
     * 测试
     */
    @RequestMapping(value = "test", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object test(String abc){
        System.out.println("123456789");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("state", 1);
        map.put("massage", "hello");
        return ajaxJson(map);
    }

    /**
     * 获取网络数据
     * @param urlPath 地址
     * @param streetNo 街道编号（0-11）
     * @return
     */
    public static String getDataByHttpRequest(String urlPath,String streetNo){
        String result=null;
        //初始化出的StringBuffer对象是一个空的对象
        StringBuffer buffer=new StringBuffer();
//        PrintWriter out = null;
        DataOutputStream out = null;
        BufferedReader in = null;
        try
        {
            // 建立链接
            URL httpUrl=new URL(urlPath);
            // 打开和URL之间的连接
            HttpURLConnection httpConn=(HttpURLConnection)httpUrl.openConnection();
            // 设置通用的请求属性
            httpConn.setUseCaches(false);  //不允许缓存
            // 发送POST请求必须设置如下两行
            httpConn.setDoOutput(true);   //需要输出
            httpConn.setDoInput(true);   //需要输入
            //设置POST方式连接
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("accept", "*/*");
            // 维持长连接
            httpConn.setRequestProperty("connection", "Keep-Alive");
            httpConn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConn.setRequestProperty("Charset", "UTF-8");
            httpConn.connect();

            // 获取URLConnection对象对应的输出流
//            out=new PrintWriter(httpConn.getOutputStream());
            out = new DataOutputStream(httpConn.getOutputStream());

            String content = "account=" + URLEncoder.encode(streetNo, "UTF-8");
            // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
            out.writeBytes(content);
            //flush输出流的缓冲
            out.flush();
            // 取得url输入流
            InputStream inputStream = httpConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            in = new BufferedReader(inputStreamReader);
            String line;
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            // 释放资源
            inputStream.close();

            result=buffer.toString();
            System.out.println("result---->"+result);
        } catch (Exception e)
        {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    //读取网络图片 设置人员照片
    public FileManage addPhoto(String urlAdd, String fileName) throws IOException {
        URL url = new URL(urlAdd);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 设置超时间为3秒
        conn.setConnectTimeout(3 * 1000);
        // 防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        // 得到输入流
        InputStream inputStream = conn.getInputStream();
        FileManage fileManage = fileManagerService.gridFSSave(inputStream, "APPLICATION/OCTET-STREAM", fileName);
        return fileManage;
    }

    public static void main(String[] args) throws Exception  {
//      请求路径
//        String url="http://test31.9joint.com/User7/DangAn/DangAnFlow.nsf/SendInfo?OpenAgent";
////        String url="http://test31.9joint.com/User7/DangAn/DangAnFlow.nsf/SendZRInfo?OpenAgent";
//        String data=getDataByHttpRequest(url, "2");
//        System.out.println("data----->" + data);
    }

    /**
     * 导入民族系统字典
     */
    @RequestMapping(value = "importNation", method = RequestMethod.POST)
    public Object importNation(@CurrentUser User user){
        if(dictService.getByValue("nation")==null){
            dictService.addDict("民族", "nation", null, 0);
        }
        Dict dict = dictService.getByValue("nation");
        if(dict!=null && getDictItems(user, "nation").size()==0){
            String[] nations = {"汉族","蒙古族","回族","藏族","维吾尔族","苗族","彝族","壮族","布依族","朝鲜族","满族","侗族","瑶族","白族","土家族",
                    "哈尼族","哈萨克族","傣族","黎族","傈僳族","佤族","畲族","高山族","拉祜族","水族","东乡族","纳西族","景颇族","柯尔克孜族",
                    "土族","达斡尔族","仫佬族","羌族","布朗族","撒拉族","毛南族","仡佬族","锡伯族","阿昌族","普米族","塔吉克族","怒族", "乌孜别克族",
                    "俄罗斯族","鄂温克族","德昂族","保安族","裕固族","京族","塔塔尔族","独龙族","鄂伦春族","赫哲族","门巴族","珞巴族","基诺族"};
            for(int i=0;i<nations.length;i++){
                System.out.println("正在导入："+nations[i]);
                dictItemService.addDefault(dict, nations[i], "nation" + i, null, 0, 0, i, null);
            }
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为nation的系统字典"));
        }
    }

    /**
     * 导入街道字典 和 相关已经存在的居民区
     */
    @RequestMapping(value = "importStreet", method = RequestMethod.POST)
    public Object importStreet(@CurrentUser User user){
        if(dictService.getByValue("street")==null){
            dictService.addDict("所在街道", "street", null, 0);
        }
        Dict dict = dictService.getByValue("street");
        if(dict!=null && getDictItems(user, "street").size()==0){

            String url="http://test31.9joint.com/User7/DangAn/DangAnFlow.nsf/SendInfo?OpenAgent";
            for(int i=0;i<=11;i++){
                System.out.println("======开始导入第"+i+"条街道");
                String data = getDataByHttpRequest(url,String.valueOf(i));
                JSONObject jsonObject = JSONObject.fromObject(data);
                String streetName = jsonObject.getString("street");
                dictItemService.addDefault(dict, streetName, "street" + i, null, 0, 0, i, null); //添加一条街道

                Set<String> residentSet = new HashSet<String>();
                String persons = jsonObject.getString("persons");
                JSONArray jsonArray = JSONArray.fromObject(persons);
                for(int j=0;j<jsonArray.size();j++){ //居民区名加进set里去重去空
                    String resName = jsonArray.getJSONObject(j).getString("txtJWH");
                    if(StringUtils.isNotEmpty(resName)){
                        residentSet.add(resName);
                    }
                }

                DictItem dictItem = dictItemService.getByValue(dict.getId(),"street"+i);
                if(dictItem!=null){
                    for(String resName:residentSet){
                        System.out.println("===正在添加【"+resName+"】居民区");
                        Resident resident = new Resident();
                        resident.setName(resName);
                        resident.setStreet(dictItem);
                        resident.setCompany(user.getCompany());
                        resident.setCreater(user);
                        residentService.save(resident);
                    }
                }
            }
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为street的系统字典"));
        }
    }

    /**
     * 导入学历
     */
    @RequestMapping(value = "importEdu", method = RequestMethod.POST)
    public Object importEdu(@CurrentUser User user){
        if(dictService.getByValue("edu")==null){
            dictService.addDict("学历", "edu", null, 0);
        }
        Dict dict = dictService.getByValue("edu");
        if(dict!=null && getDictItems(user, "edu").size()==0){
            dictItemService.addDefault(dict, "初中", "edu1", null, 0, 0, 1, null);
            dictItemService.addDefault(dict, "高中或中专", "edu2", null, 0, 0, 2, null);
            dictItemService.addDefault(dict, "技校", "edu3", null, 0, 0, 3, null);
            dictItemService.addDefault(dict, "大专", "edu4", null, 0, 0, 4, null);
            dictItemService.addDefault(dict, "本科", "edu5", null, 0, 0, 5, null);
            dictItemService.addDefault(dict, "研究生", "edu6", null, 0, 0, 6, null);
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为edu的系统字典"));
        }
    }

    /**
     * 导入人员性质（书记）
     */
    @RequestMapping(value = "importPersonNature", method = RequestMethod.POST)
    public Object importPersonNature(@CurrentUser User user){
        if(dictService.getByValue("personNature")==null){
            dictService.addDict("人员性质（书记）", "personNature", null, 0);
        }
        Dict dict = dictService.getByValue("personNature");
        if(dict!=null && getDictItems(user, "personNature").size()==0){
            dictItemService.addDefault(dict, "公务员", "gongwuyuan", null, 0, 0, 1, null);
            dictItemService.addDefault(dict, "参公", "cangong", null, 0, 0, 2, null);
            dictItemService.addDefault(dict, "事业编制", "shiyebianzhi", null, 0, 0, 3, null);
            dictItemService.addDefault(dict, "享受事业编制", "xiangshoushiyebianzhi", null, 0, 0, 4, null);
            dictItemService.addDefault(dict, "书记后备", "shujihoubei", null, 0, 0, 5, null);
            dictItemService.addDefault(dict, "退休返聘", "tuixiufanpin", null, 0, 0, 6, null);
            dictItemService.addDefault(dict, "其他", "qita", null, 0, 0, 7, null);
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为personNature的系统字典"));
        }
    }

    /**
     * 导入人员性质（主任）
     */
    @RequestMapping(value = "importDirectorPersonNature", method = RequestMethod.POST)
    public Object importDirectorPersonNature(@CurrentUser User user){
        if(dictService.getByValue("directorPersonNature")==null){
            dictService.addDict("人员性质（主任）", "directorPersonNature", null, 0);
        }
        Dict dict = dictService.getByValue("directorPersonNature");
        if(dict!=null && getDictItems(user, "directorPersonNature").size()==0){
            dictItemService.addDefault(dict, "主任", "directorNature0", null, 0, 0, 0, null);
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为personNature的系统字典"));
        }
    }

    /**
     * 导入工作性质
     */
    @RequestMapping(value = "importWorkNature", method = RequestMethod.POST)
    public Object importWorkNature(@CurrentUser User user){
        if(dictService.getByValue("workNature")==null){
            dictService.addDict("工作性质", "workNature", null, 0);
        }
        Dict dict = dictService.getByValue("workNature");
        if(dict!=null && getDictItems(user, "workNature").size()==0){
            dictItemService.addDefault(dict, "居民区", "workNature0", null, 0, 0, 0, null);
            return ajaxJson(resultService.success("导入成功"));
        }else{
            return ajaxJson(resultService.fail("导入失败，或已经存在了关键字为workNature的系统字典"));
        }
    }

    /**
     * 导入人员
     */
    @RequestMapping(value = "importPerson", method = RequestMethod.POST)
    public Object importPerson(@CurrentUser User user) throws IOException {

        if(personService.getAll().size()>0){
            return ajaxJson(resultService.fail("请先删除数据库中的书记人员"));
        }

        Duty duty = null;
        for(Duty duty1:user.getDutySet()){
            duty=duty1;
            break;
        }

        //从0-11 工12个街道
        String url="http://test31.9joint.com/User7/DangAn/DangAnFlow.nsf/SendInfo?OpenAgent";
        for(int j=0;j<=11;j++){
            System.out.println("======共12条，开始添加第"+j+"条街道，");
            String data=getDataByHttpRequest(url,String.valueOf(j));
            JSONObject jsonObject = JSONObject.fromObject(data);
            String persons = jsonObject.getString("persons");
            JSONArray jsonArray = JSONArray.fromObject(persons);
            for(int i=0;i<jsonArray.size();i++){
                System.out.println("===开始添加第"+(i+1)+"名人员，共"+(jsonArray.size()+1)+"名");
                JSONObject json = jsonArray.getJSONObject(i);

                Person person = new Person();
                person.setCreater(user);
                person.setCompany(user.getCompany());
                person.setName(json.getString("txtName")); //姓名
                person.setGender(json.getString("txtSex")); //性别
                person.setBirthday(DataUtil.StringToDate(json.getString("datBorn"), "yyyy年MM月")); //出生日期
                person.setNation(dictItemService.getByName(json.getString("txtNation"))); //民族
                person.setOrigin(json.getString("txtNative")); //籍贯
                person.setBirthPlace(json.getString("txtBorn")); //出生地
                person.setHealth(json.getString("txtHealth")); //健康状况
                person.setJoinPartyDate(DataUtil.StringToDate(json.getString("datRD"), "yyyy年MM月")); //入党时间
                person.setJoinWorkDate(DataUtil.StringToDate(json.getString("datWork"), "yyyy年MM月")); //参加工作时间
                person.setTxtTecDuty(json.getString("txtTecDuty")); //专业技术服务
                person.setSpecialty(json.getString("txtSpec")); //熟悉专业有何专长
                person.setWorkNature(dictItemService.getByName(json.getString("txtWorkType"))); //工作性质
                person.setPersonNature(dictItemService.getByName(json.getString("txtType"))); //人员性质
                person.setHighEdu(dictItemService.getByName(json.getString("txtEducation"))); //最高学历
                person.setAllDayEdu(dictItemService.getByName(json.getString("txtEducation_1"))); //全日制教育
                person.setJobEdu(dictItemService.getByName(json.getString("txtEducation_2"))); //在职教育
                person.setAllDaySchool(json.getString("txtSpec_1")); //全日制教育-毕业院校系及专业
                person.setJobSchool(json.getString("txtSpec_2")); //在职教育-毕业院校系及专业
                person.setNowPost(json.getString("txtDuty")); //现任职务
                person.setNowPostDate(DataUtil.StringToDate(json.getString("datStart"), "yyyy年MM月")); //现任职时间
                person.setArea(json.getString("txtArea")); //所在区
                person.setStreet(dictItemService.getByName(json.getString("txtStreet"))); //所在街道
                person.setResident(residentService.getByName(json.getString("txtJWH"))); //居民区
                person.setCreateTime(DataUtil.StringToDate(json.getString("datApprove"), "yyyy年MM月dd日")); //创建时间
                person.setReward(json.getString("txtInfo")); //奖惩情况

                String yjt = json.getString("txtYJT");
                if("否".equals(yjt)){
                    person.setShoulder(0);
                }else{
                    person.setShoulder(1);
                    person.setShoulderDet(json.getString("txtYJTs"));
                }


                //学习经历
                List<Study> studyList = new ArrayList<Study>();

                Study study1 = new Study();
                study1.setStartDate(DataUtil.StringToDate(json.getString("datStart_1"), "yyyy年MM月")); //开始时间
                study1.setEndDate(DataUtil.StringToDate(json.getString("datEnd_1"), "yyyy年MM月")); //结束时间
                study1.setSchool(json.getString("txtSchool_1")); //学校
                study1.setAcademicDegree(json.getString("txtStu_1")); //所获学位
                studyService.save(study1);
                studyList.add(study1);

                Study study2 = new Study();
                study2.setStartDate(DataUtil.StringToDate(json.getString("datStart_2"), "yyyy年MM月")); //开始时间
                study2.setEndDate(DataUtil.StringToDate(json.getString("datEnd_2"), "yyyy年MM月")); //结束时间
                study2.setSchool(json.getString("txtSchool_2")); //学校
                study2.setAcademicDegree(json.getString("txtStu_2")); //所获学位
                studyService.save(study2);
                studyList.add(study2);

                Study study3 = new Study();
                study3.setStartDate(DataUtil.StringToDate(json.getString("datStart_3"), "yyyy年MM月")); //开始时间
                study3.setEndDate(DataUtil.StringToDate(json.getString("datEnd_3"), "yyyy年MM月")); //结束时间
                study3.setSchool(json.getString("txtSchool_3")); //学校
                study3.setAcademicDegree(json.getString("txtStu_3")); //所获学位
                studyService.save(study3);
                studyList.add(study3);

                Study study4 = new Study();
                study4.setStartDate(DataUtil.StringToDate(json.getString("datStart_4"), "yyyy年MM月")); //开始时间
                study4.setEndDate(DataUtil.StringToDate(json.getString("datEnd_4"), "yyyy年MM月")); //结束时间
                study4.setSchool(json.getString("txtSchool_4")); //学校
                study4.setAcademicDegree(json.getString("txtStu_4")); //所获学位
                studyService.save(study4);
                studyList.add(study4);

                Study study5 = new Study();
                study5.setStartDate(DataUtil.StringToDate(json.getString("datStart_5"), "yyyy年MM月")); //开始时间
                study5.setEndDate(DataUtil.StringToDate(json.getString("datEnd_5"), "yyyy年MM月")); //结束时间
                study5.setSchool(json.getString("txtSchool_5")); //学校
                study5.setAcademicDegree(json.getString("txtStu_5")); //所获学位
                studyService.save(study5);
                studyList.add(study5);

                person.setStudyList(studyList);


                //工作时间
                List<Work> workList = new ArrayList<Work>();
                for(int k=1;k<=10;k++){ //有10条
                    Work work = new Work();
                    work.setWorkStartDate(DataUtil.StringToDate(json.getString("datWStart_"+k), "yyyy年MM月")); //开始时间
                    work.setWorkEndDate(DataUtil.StringToDate(json.getString("datWEnd_"+k), "yyyy年MM月")); //结束时间
                    work.setWorkDept(json.getString("txtUnit_"+k)); //工作单位及部门
                    work.setWorkPost(json.getString("txtDuty_"+k)); //职务
                    work.setProveUser(json.getString("txtZMR_"+k)); //证明人
                    workService.save(work);
                    workList.add(work);
                }
                person.setWorkList(workList);


                //家庭信息
                List<Family> familyList = new ArrayList<Family>();
                for(int m=1;m<=4;m++){ //4条
                    Family family = new Family();
                    family.setNickName(json.getString("txtCW_"+m)); //称谓
                    family.setFamilyName(json.getString("txtName_" + m)); //姓名
                    family.setFamilyBirthday(DataUtil.StringToDate(json.getString("datBorn_" + m), "yyyy年MM月")); //出生年月
                    family.setPoliticalStatus(json.getString("txtZZ_"+m)); //政治面貌
                    family.setFamilyEdu(json.getString("txtXL_"+m)); //学历学位
                    family.setFamilyJob(json.getString("txtUD_"+m)); //工作单位及职务
                    familyService.save(family);
                    familyList.add(family);
                }
                person.setFamilyList(familyList);
                person.setFamilyAddress(json.getString("txtFamilyAddress")); //家庭地址
                person.setPhone(json.getString("txtPhone")); //联系电话
                person.setZipCode(json.getString("txtPostCode")); //邮编

                String unid = json.getString("UNID");
                String fileName = json.getString("txtFileName");
                String fileUrl = "http://test31.9joint.com/User7/DangAn/DangAn.nsf/0/"+unid+"/$FILE/"+URLEncoder.encode(fileName,"UTF-8");
                FileManage fileManage = addPhoto(fileUrl,fileName);
                person.setPhoto(fileManage);

                personService.commit(person, "com.fz.us.oa.core.entity.com.Person", duty);
            }
        }
        return ajaxJson(resultService.success("导入成功"));
    }

    /**
     * 导入主任
     */
    @RequestMapping(value = "importDirector", method = RequestMethod.POST)
    public Object importDirector(@CurrentUser User user) throws IOException{

        if(directorService.getAll().size()>0){
            return ajaxJson(resultService.fail("请先删除数据库中的所有主任"));
        }

        Duty duty = null;
        for(Duty duty1:user.getDutySet()){
            duty=duty1;
            break;
        }

        //从0-11 工12个街道
        String url="http://test31.9joint.com/User7/DangAn/DangAnFlow.nsf/SendZRInfo?OpenAgent";
        for(int j=0;j<=11;j++){
            System.out.println("======共12条，开始添加第"+j+"条街道，");
            String data=getDataByHttpRequest(url,String.valueOf(11));
            JSONObject jsonObject = JSONObject.fromObject(data);
            String persons = jsonObject.getString("persons");
            JSONArray jsonArray = JSONArray.fromObject(persons);
            for(int i=0;i<jsonArray.size();i++){
                System.out.println("===开始添加第"+(i+1)+"名人员，共"+(jsonArray.size()+1)+"名");
                JSONObject json = jsonArray.getJSONObject(i);

                Director person = new Director();
                person.setCreater(user);
                person.setCompany(user.getCompany());
                person.setName(json.getString("txtName")); //姓名
                person.setGender(json.getString("txtSex")); //性别
                person.setBirthday(DataUtil.StringToDate(json.getString("datBorn"), "yyyy年MM月")); //出生日期
                person.setNation(dictItemService.getByName(json.getString("txtNation"))); //民族
                person.setOrigin(json.getString("txtNative")); //籍贯
                person.setBirthPlace(json.getString("txtBorn")); //出生地
                person.setHealth(json.getString("txtHealth")); //健康状况
                person.setJoinPartyDate(DataUtil.StringToDate(json.getString("datRD"), "yyyy年MM月")); //入党时间
                person.setJoinWorkDate(DataUtil.StringToDate(json.getString("datWork"), "yyyy年MM月")); //参加工作时间
                person.setTxtTecDuty(json.getString("txtTecDuty")); //专业技术服务
                person.setSpecialty(json.getString("txtSpec")); //熟悉专业有何专长
                person.setWorkNature(dictItemService.getByName(json.getString("txtWorkType"))); //工作性质
                person.setPersonNature(dictItemService.getByName(json.getString("txtType"))); //人员性质
                person.setHighEdu(dictItemService.getByName(json.getString("txtEducation"))); //最高学历
                person.setAllDayEdu(dictItemService.getByName(json.getString("txtEducation_1"))); //全日制教育
                person.setJobEdu(dictItemService.getByName(json.getString("txtEducation_2"))); //在职教育
                person.setAllDaySchool(json.getString("txtSpec_1")); //全日制教育-毕业院校系及专业
                person.setJobSchool(json.getString("txtSpec_2")); //在职教育-毕业院校系及专业
                person.setNowPost(json.getString("txtDuty")); //现任职务
                person.setNowPostDate(DataUtil.StringToDate(json.getString("datStart"), "yyyy年MM月")); //现任职时间
                person.setArea(json.getString("txtArea")); //所在区
                person.setStreet(dictItemService.getByName(json.getString("txtStreet"))); //所在街道
                person.setResident(residentService.getByName(json.getString("txtJWH"))); //居民区
                person.setCreateTime(DataUtil.StringToDate(json.getString("datApprove"), "yyyy年MM月dd日")); //创建时间
                person.setReward(json.getString("txtInfo")); //奖惩情况
                person.setZzmm(json.getString("txtZZMM")); //政治面貌（主任特有的！！！）

                String yjt = json.getString("txtYJT");
                if("否".equals(yjt)){
                    person.setShoulder(0);
                }else{
                    person.setShoulder(1);
                    person.setShoulderDet(json.getString("txtYJTs"));
                }


                //学习经历
                List<Study> studyList = new ArrayList<Study>();

                Study study1 = new Study();
                study1.setStartDate(DataUtil.StringToDate(json.getString("datStart_1"), "yyyy年MM月")); //开始时间
                study1.setEndDate(DataUtil.StringToDate(json.getString("datEnd_1"), "yyyy年MM月")); //结束时间
                study1.setSchool(json.getString("txtSchool_1")); //学校
                study1.setAcademicDegree(json.getString("txtStu_1")); //所获学位
                studyService.save(study1);
                studyList.add(study1);

                Study study2 = new Study();
                study2.setStartDate(DataUtil.StringToDate(json.getString("datStart_2"), "yyyy年MM月")); //开始时间
                study2.setEndDate(DataUtil.StringToDate(json.getString("datEnd_2"), "yyyy年MM月")); //结束时间
                study2.setSchool(json.getString("txtSchool_2")); //学校
                study2.setAcademicDegree(json.getString("txtStu_2")); //所获学位
                studyService.save(study2);
                studyList.add(study2);

                Study study3 = new Study();
                study3.setStartDate(DataUtil.StringToDate(json.getString("datStart_3"), "yyyy年MM月")); //开始时间
                study3.setEndDate(DataUtil.StringToDate(json.getString("datEnd_3"), "yyyy年MM月")); //结束时间
                study3.setSchool(json.getString("txtSchool_3")); //学校
                study3.setAcademicDegree(json.getString("txtStu_3")); //所获学位
                studyService.save(study3);
                studyList.add(study3);

                Study study4 = new Study();
                study4.setStartDate(DataUtil.StringToDate(json.getString("datStart_4"), "yyyy年MM月")); //开始时间
                study4.setEndDate(DataUtil.StringToDate(json.getString("datEnd_4"), "yyyy年MM月")); //结束时间
                study4.setSchool(json.getString("txtSchool_4")); //学校
                study4.setAcademicDegree(json.getString("txtStu_4")); //所获学位
                studyService.save(study4);
                studyList.add(study4);

                Study study5 = new Study();
                study5.setStartDate(DataUtil.StringToDate(json.getString("datStart_5"), "yyyy年MM月")); //开始时间
                study5.setEndDate(DataUtil.StringToDate(json.getString("datEnd_5"), "yyyy年MM月")); //结束时间
                study5.setSchool(json.getString("txtSchool_5")); //学校
                study5.setAcademicDegree(json.getString("txtStu_5")); //所获学位
                studyService.save(study5);
                studyList.add(study5);

                person.setStudyList(studyList);


                //工作时间
                List<Work> workList = new ArrayList<Work>();
                for(int k=1;k<=10;k++){ //有10条
                    Work work = new Work();
                    work.setWorkStartDate(DataUtil.StringToDate(json.getString("datWStart_"+k), "yyyy年MM月")); //开始时间
                    work.setWorkEndDate(DataUtil.StringToDate(json.getString("datWEnd_"+k), "yyyy年MM月")); //结束时间
                    work.setWorkDept(json.getString("txtUnit_"+k)); //工作单位及部门
                    work.setWorkPost(json.getString("txtDuty_"+k)); //职务
                    work.setProveUser(json.getString("txtZMR_"+k)); //证明人
                    workService.save(work);
                    workList.add(work);
                }
                person.setWorkList(workList);


                //家庭信息
                List<Family> familyList = new ArrayList<Family>();
                for(int m=1;m<=4;m++){ //4条
                    Family family = new Family();
                    family.setNickName(json.getString("txtCW_"+m)); //称谓
                    family.setFamilyName(json.getString("txtName_" + m)); //姓名
                    family.setFamilyBirthday(DataUtil.StringToDate(json.getString("datBorn_" + m), "yyyy年MM月")); //出生年月
                    family.setPoliticalStatus(json.getString("txtZZ_"+m)); //政治面貌
                    family.setFamilyEdu(json.getString("txtXL_"+m)); //学历学位
                    family.setFamilyJob(json.getString("txtUD_"+m)); //工作单位及职务
                    familyService.save(family);
                    familyList.add(family);
                }
                person.setFamilyList(familyList);
                person.setFamilyAddress(json.getString("txtFamilyAddress")); //家庭地址
                person.setPhone(json.getString("txtPhone")); //联系电话
                person.setZipCode(json.getString("txtPostCode")); //邮编

                String unid = json.getString("UNID");
                String fileName = json.getString("txtFileName");
                String fileUrl = "http://test31.9joint.com/User7/DangAn/DangAn.nsf/0/"+unid+"/$FILE/"+URLEncoder.encode(fileName,"UTF-8");
                FileManage fileManage = addPhoto(fileUrl,fileName);
                person.setPhoto(fileManage);

                directorService.commit(person, "com.fz.us.oa.core.entity.com.Director", duty);
            }
        }
        System.out.println("======导入成功");
        return ajaxJson(resultService.success("导入成功"));
    }

}
